s = "Hello, Python world!"

# 1. 字符串长度计算
length = len(s)
print("1. 字符串长度:", length)
# 2. 计算字符串中"o"字符的个数
count_o = s.count('o')
print("2. 'o'的个数:", count_o)
# 3. 转换为大写
upper_s = s.upper()
print("3. 大写:", upper_s)
# 4. 转换为小写
lower_s = s.lower()
print("4. 小写:", lower_s)
# 5. 查找"Python"的位置
pos_python = s.find("Python")
print("5. 'Python'的位置:", pos_python)
# 6. 替换"world"为"universe"
replaced_s = s.replace("world", "universe")
print("6. 替换后:", replaced_s)
# 7. 检查是否以"Hello"开始
starts = s.startswith("Hello")
print("7. 是否以'Hello'开始:", starts)
# 8. 检查是否以"!"结尾
ends = s.endswith("!")
print("8. 是否以'!'结尾:", ends)
# 9. 以","为分隔符拆分字符串
split_s = s.split(",")
print("9. 拆分结果:", split_s)
# 10. 去除首尾的"!"
trimmed_s = s.strip("!")
print("10. 去除首尾'!'后:", trimmed_s)
# 11. 字符串反转
reversed_s = s[::-1]
print("11. 反转后:", reversed_s)
# 12. 切片获取"Python"
python_slice = s[7:13]
print("12. 'Python'切片:", python_slice)
# 13. 删除"Hello,"
removed_hello = s.replace("Hello, ", "")
print("13. 删除'Hello,'后:", removed_hello)
# 14. 拼接两个字符串
concatenated = "Hello, " + "World!"
print("14. 拼接结果:", concatenated)
# 15. 使用f-string合并字符串和数字
f_merged = f"{s} {2021}"
print("15. f-string合并:", f_merged)
# 16. 找出'a'第一次出现的位置
pos_a = s.find('a')
print("16. 'a'的首次位置:", pos_a)
# 17. 找出'e'最后一次出现的位置
pos_e_last = s.rfind('e')
print("17. 'e'的最后位置:", pos_e_last)
# 18. 计算空格数量
space_count = s.count(' ')
print("18. 空格数量:", space_count)
# 19. 循环遍历每个字符
print("19. 遍历字符:")
for char in s:
    print(char, end=' ')
print()
# 20. 转为列表形式
char_list = list(s)
print("20. 转为列表:", char_list)
# 21. 字符串排序
sorted_chars = sorted(s)
sorted_str = ''.join(sorted_chars)
print("21. 排序后的字符串:", sorted_str)
# 22. 判断是否为数字
is_digit = s.isdigit()
print("22. 是否为数字:", is_digit)
# 23. 连接列表元素
joined_list = ' '.join(['Hello,', 'Python', 'World!'])
print("23. 列表连接结果:", joined_list)
# 24. 使用%操作符格式化
formatted = "Number: %d" % 100
print("24. %格式化结果:", formatted)
# 25. 检查是否全为小写
is_lower = s.islower()
print("25. 是否全小写:", is_lower)
# 26. 检查是否全为大写
is_upper = s.isupper()
print("26. 是否全大写:", is_upper)
# 27. 数字列表转字符串
num_list = [1, 2, 3]
num_str = '-'.join(map(str, num_list))
print("27. 数字列表转字符串:", num_str)
# 28. 找出所有"o"的位置
o_positions = [i for i, char in enumerate(s) if char == 'o']
print("28. 所有'o'的位置:", o_positions)
# 29. 替换第一个"o"为"O"
first_o_replaced = s.replace('o', 'O', 1)
print("29. 替换第一个'o'后:", first_o_replaced)
# 30. 字符串插入操作
insert_pos = 7
inserted_s = s[:insert_pos] + "amazing " + s[insert_pos:]
print("30. 插入后的字符串:", inserted_s)
##############################################################################
s = "Python is great!"
s2 = "A man, a plan, a canal, Panama!"
s3 = "encryption"
# 1. 字符串反转
reversed_s = ''
for i in range(len(s)-1, -1, -1):
    reversed_s += s[i]
print("1. 反转字符串:", reversed_s)
# 2. 计算字符 't' 出现次数
count = 0
for char in s:
    if char == 't':
        count += 1
print("2. 't' 的出现次数:", count)
# 3. 替换空格为下划线
ss = ''
for char in s:
    if char == ' ':
        ss += '_'
    else:
        ss += char
print("3. 替换空格后:", ss)
# 4. 检查回文（忽略大小写、标点和空格）
cleaned_s2 = ''.join(e.lower() for e in s2 if e.isalnum())
is_palindrome = cleaned_s2 == cleaned_s2[::-1]
print("4. 是否为回文:", is_palindrome)
# 5. 查找子字符串 "is" 的位置
index = -1
for i in range(len(s) - 1):
    if s[i] == 'i' and s[i+1] == 's':
        index = i
        break
print("5. 'is' 的位置:", index)
# 6. 在第一个 't' 后插入 "_inserted"
t_index = s.index('t')
s_inserted = s[:t_index+1] + "_inserted" + s[t_index+1:]
print("6. 插入后的字符串:", s_inserted)
# 7. 替换第一次出现的 "great" 为 "awesome"
s_replaced = s.replace("great", "awesome", 1)
print("7. 替换后的字符串:", s_replaced)
# 8. 切片与拼接（前后半部分交换）
mid = len(s) // 2
s_swapped = s[mid:] + s[:mid]
print("8. 交换后的字符串:", s_swapped)
# 9. 列表生成式（单词首字母大写）
words = s.split()
capitalized = [word.capitalize() for word in words]
print("9. 首字母大写列表:", capitalized)
# 10. 字符替换加密
def encrypt(s):
    mapping = {chr(ord('a') + i): chr(ord('m') + i) for i in range(13)}
    mapping.update({chr(ord('n') + i): chr(ord('a') + i) for i in range(13)})
    return ''.join(mapping.get(c, c) for c in s.lower())
encrypted = encrypt(s3)
print("10. 加密后的字符串:", encrypted)